/*
 * mex_main.cpp
 *
 *  Created on: 13/01/2012
 *      Author: will
 */

#include <ctime>
#include <string>
using std::string;

#include "mex_main.h"
#include <mex.h>


void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

	// seed random number gen
	//srand ( time(NULL) );

	// define persistent static pointers
	static CatGraph *catGraph;

	// check first argument is a string
	const char *function_name;
	if (!mxIsChar(prhs[0]) || mxGetM(prhs[0]) != 1 || mxIsClass(prhs[0],
			"sparse") || mxIsComplex(prhs[0])) {
		mexErrMsgTxt(
				"must specify function name as row vector string, first argument");
	}
	function_name = mxArrayToString(prhs[0]);
	mexPrintf("Function: %s\n",function_name);

	if (!strcmp(function_name, "deserializeGraph")) {
		checkArgs_deserializeGraph(&nrhs, prhs);
		catGraph = deserializeGraph(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "serializeGraph")) {
		checkArgs_serializeGraph(&nrhs, prhs);
		serializeGraph(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "initialiseGraph")) {
		checkArgs_initialiseGraph(&nrhs, prhs);
		catGraph = initialiseGraph(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "initialiseVision")) {
		checkArgs_initialiseVision(&nrhs, prhs);
		initialiseVision(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "initialiseTrajectory")) {
		checkArgs_initialiseTrajectory(&nrhs, prhs);
		initialiseTrajectory(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "initialiseParticles")) {
		checkArgs_initialiseParticles(&nrhs, prhs);
		initialiseParticles(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "addNode")) {
		checkArgs_addNode(&nrhs, prhs);
		addNode(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "updateInformation")) {
		checkArgs_updateInformation(&nrhs, prhs);
		updateInformation(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "updateInformationOnline")) {
		checkArgs_updateInformationOnline(&nrhs, prhs);
		updateInformationOnline(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "pruneNode")) {
		checkArgs_pruneNode(&nrhs, prhs);
		pruneNode(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "pruneNodeOnline")) {
		checkArgs_pruneNodeOnline(&nrhs, prhs);
		pruneNodeOnline(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "updateParticlePositions")) {
		checkArgs_updateParticlePositions(&nrhs, prhs);
		updateParticlePositions(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "updateParticleWeightsOnline")) {
		checkArgs_updateParticleWeightsOnline(&nrhs, prhs);
		updateParticleWeightsOnline(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "updateParticleWeights")) {
		checkArgs_updateParticleWeights(&nrhs, prhs);
		updateParticleWeights(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "calculateESS")) {
		checkArgs_calculateESS(&nrhs, prhs);
		calculateESS(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "calculateHypothesis")) {
		checkArgs_calculateHypothesis(&nrhs, prhs);
		calculateHypothesis(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "addLoopClosure")) {
		checkArgs_addLoopClosure(&nrhs, prhs);
		addLoopClosure(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "resampleParticles")) {
		checkArgs_resampleParticles(&nrhs, prhs);
		resampleParticles(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "buildCodebookFromMap")) {
		checkArgs_buildCodebookFromMap(&nrhs, prhs);
		buildCodebookFromMap(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "buildChowLiuFromMap")) {
		checkArgs_buildChowLiuFromMap(&nrhs, prhs);
		buildChowLiuFromMap(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "recomputeWords")) {
		checkArgs_recomputeWords(&nrhs, prhs);
		recomputeWords(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "recomputeInformation")) {
		checkArgs_recomputeInformation(&nrhs, prhs);
		recomputeInformation(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "printNodes")) {
		checkArgs_printNodes(&nrhs, prhs);
		printNodes(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "printEdges")) {
		checkArgs_printEdges(&nrhs, prhs);
		printEdges(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "printParticles")) {
		checkArgs_printParticles(&nrhs, prhs);
		printParticles(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "printLocalGraph")) {
		checkArgs_printLocalGraph(&nrhs, prhs);
		printLocalGraph(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "printInformation")) {
		checkArgs_printInformation(&nrhs, prhs);
		printInformation(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "outputLocalGraph")) {
		checkArgs_outputLocalGraph(&nrhs, prhs);
		outputLocalGraph(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "outputLocalUpdate")) {
		checkArgs_outputLocalUpdate(&nrhs, prhs);
		outputLocalUpdate(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "outputLocalHypothesis")) {
		checkArgs_outputLocalHypothesis(&nrhs, prhs);
		outputLocalHypothesis(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "outputNodes")) {
		checkArgs_outputNodes(&nrhs, prhs);
		outputNodes(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "outputEdges")) {
		checkArgs_outputEdges(&nrhs, prhs);
		outputEdges(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "outputParticles")) {
		checkArgs_outputParticles(&nrhs, prhs);
		outputParticles(plhs, prhs, catGraph);
		return;
	}

	if (!strcmp(function_name, "outputPlan")) {
		checkArgs_outputPlan(&nrhs, prhs);
		outputPlan(plhs, prhs, catGraph);
		return;
	}

	mexErrMsgTxt("Function name did not match any known functions");
	return;

}

